package com.ming_za.od_updater.core.window.page;

import com.ming_za.od_updater.core.window.LoadingState;
import com.ming_za.od_updater.core.updater.StateBridge;
import com.ming_za.od_updater.core.window.PageManager;
import javax.swing.*;
import java.awt.*;

public class FailPage implements IPage{

    @Override
    public void renderContent(JPanel panel) {

        panel.add(Box.createRigidArea(new Dimension(0, 20)));

        // 状态标签
        JLabel statusLabel = new JLabel(getStateText(), JLabel.CENTER);
        statusLabel.setFont(statusLabel.getFont().deriveFont(16f));
        statusLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
        panel.add(statusLabel);

        panel.add(Box.createRigidArea(new Dimension(0, 10)));

        JPanel btnPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));

        JButton retryButton = new JButton(PageManager.retryText);
        retryButton.addActionListener(e -> PageManager.setPage(new CheckUpdatePage()));
        retryButton.setPreferredSize(new Dimension(60, 25));
        btnPanel.add(retryButton);

        JButton continueButton = new JButton(PageManager.continueText);
        continueButton.addActionListener(e -> StateBridge.setLoadingState(LoadingState.CONTINUE));
        continueButton.setPreferredSize(new Dimension(80, 25));
        btnPanel.add(continueButton);

        JButton closeButton = new JButton(PageManager.shutdownText);
        closeButton.addActionListener(e -> StateBridge.setLoadingState(LoadingState.SHUTDOWN));
        closeButton.setPreferredSize(new Dimension(80, 25));
        btnPanel.add(closeButton);

        panel.add(btnPanel);

        panel.add(Box.createRigidArea(new Dimension(0, 10)));

    }

    private String getStateText(){
        switch (StateBridge.getUpdateState()){
            case FAILED:
                return PageManager.failedText;
            case UN_CONNECT:
                return PageManager.unConnectText;
        }
        return "意外错误";
    }
}
